class Product:
    def __init__(self, product_id, name, price, stock):
        self.product_id = product_id
        self.name = name
        self.price = price
        self._stock = stock
        self.observers = []

    # 观察者相关方法（同原始代码）
    @property
    def stock(self):
        return self._stock

    @stock.setter
    def stock(self, value):
        if value != self._stock:
            self._stock = value
            self.notify_observers()

    def add_observer(self, observer):
        self.observers.append(observer)

    def remove_observer(self, observer):
        self.observers.remove(observer)

    def notify_observers(self):
        for observer in self.observers:
            observer.update(self)


class ProductFactory:
    @staticmethod
    def create_product(product_id, name, price, stock):
        if price <= 0:
            raise ValueError("价格必须为正数")
        if stock < 0:
            raise ValueError("库存不能为负")
        return Product(product_id, name, price, stock)

# models/order.py
